#include<bits/stdc++.h>
#define LL int
#define SZ(x) ((LL)(x.size()))
#define all(x) (x).begin(),(x).end()
using namespace std;
inline LL read(){
  LL q=0,w=1;char ch=getchar();
  while(ch>'9' || ch<'0'){if(ch=='-')w=-1;ch=getchar();}
  while(ch>='0'&&ch<='9'){q=q*10+(ch-'0');ch=getchar();}
  return q*w;
}
void write(LL x){
  if(x<0){putchar('-');x=(-x);}
  if(x>9)write(x/10);
  putchar('0'+x%10);
}
inline void writeln(LL x){write(x);puts("");}
inline void writecs(LL x){write(x);putchar(' ');}

#define pb push_back

inline void lsh(vector<LL>&vc){
  sort(all(vc));
  vc.erase(unique(all(vc)),vc.end());
  return ;
}

const LL N = 8000+95 , M = 30000+95;
LL n,m,b[N];struct pt{LL x,y;}a[N];bitset<(M<<1)>ok;

inline LL mabs(LL x){return ((x<0)?(-x):(x));}
inline LL dist(pt x,pt y){return mabs(x.x-y.x)+mabs(x.y-y.y);}

vector<LL>ord[N];LL dis[N][N],top[N];

struct info{LL x,y,d;};bitset<N>dp[N];map<LL,LL>MP[M];vector<LL>E[N];

int main(){
  freopen("chess.in","r",stdin);
  freopen("chess.out","w",stdout);
  LL TC=read();
  while(TC--){
    n=read();m=read();for(LL x=0;x<=m+1;x++)MP[x].clear();
    for(LL i=1;i<=n;i++){LL x=read(),y=read();a[i]=(pt){x,y};MP[x][y]=1;}
    for(LL i=1;i<=n;i++)b[i]=read();

    for(LL u=1;u<=(m<<1);u++)ok[u]=0;
    for(LL u=2;u<=(m<<1);u++)
      if(!ok[u])for(LL i=(u<<1);i<=(m<<1);i+=u)ok[i]=1;

    for(LL u=2;u<=(m<<1);u++){
      if((!ok[u])&&((u<=10)||(u%20==3)))ok[u]=1;
      else {ok[u]=0;}
    }

    for(LL i=1;i<=n;i++)for(LL j=i+1;j<=n;j++)dis[i][j]=dis[j][i]=dist(a[i],a[j]);

    if(n<=100){
      LL ans=0;
      for(LL i=1;i<=n;i++)
	for(LL j=i+1;j<=n;j++)
	  for(LL k=j+1;k<=n;k++){
	    LL d0=dis[i][j],d1=dis[j][k],d2=dis[i][k];
	    if(d0>d1)swap(d0,d1);
	    if(d1>d2)swap(d1,d2);
	    if(d0>d1)swap(d0,d1);
	    if(ok[d1])ans+=b[i]+b[j]+b[k];
	  }
      writeln(ans);
      continue;
    }
    
    for(LL s=1;s<=n;s++){
      ord[s].resize(n);for(LL i=0;i<n;i++)ord[s][i]=i+1;
      sort(all(ord[s]),[&](LL x,LL y){return dis[s][x]<dis[s][y];});
      top[s]=0;dp[s].reset();
    }
    /*    for(LL i=1;i<=n;i++)
      for(LL j=1;j<=n;j++)cout<<" i = "<<i<<" j = "<<j<<" dis[i][j] = "<<dis[i][j]<<endl;*/
    vector<info>vc;
    for(LL i=1;i<=n;i++)
      for(LL j=i+1;j<=n;j++)
	if(ok[dis[i][j]])vc.push_back((info){i,j,dis[i][j]});
    sort(all(vc),[&](info x,info y){return x.d<y.d;});
    LL ans=0;
    for(LL l=0,r=0;l<SZ(vc);l=r+1){
      r=l;while(r+1<SZ(vc)&&vc[r+1].d==vc[l].d)r++;
      LL D=vc[l].d;vector<LL>deg(n+2,0);
      for(LL x=1;x<=n;x++)
	while(top[x]<n&&dis[x][ord[x][top[x]]]<=D){dp[x][ord[x][top[x]]]=1;top[x]++;}

      vector<LL>node;
      for(LL i=l;i<=r;i++){LL x=vc[i].x,y=vc[i].y;E[x].clear();E[y].clear();node.pb(x);node.pb(y);}
      for(LL i=l;i<=r;i++){
	LL x=vc[i].x,y=vc[i].y;auto U=(dp[x]&dp[y]);
	LL sum=(dp[x].count()+dp[y].count()-(U.count()<<1));
	ans+=sum;
	//	cout<<" -> ans = "<<ans<<" sum = "<<sum<<endl;

	E[x].push_back(y);E[y].push_back(x);
      }
      lsh(node);
      for(auto x:node){
	//	sort(all(E[x]),[&](LL p,LL q){return atan2(a[x].y-a[p].y,a[x].x-a[p].x)<atan2(a[x].y-a[q].y,a[x].x-a[q].x);});
	for(LL i=0;i<SZ(E[x]);i++)
	  for(LL j=i+1;j<SZ(E[x]);j++){
	    if(dis[E[x][i]][E[x][j]]<D)ans++;
	    if(dis[E[x][i]][E[x][j]]>D)ans--;
	  }
      }
    }
    LL cnt=0;
    for(LL i=1;i<=n;i++){
      LL x=a[i].x,y=a[i].y;
      //      cout<<" x = "<<x<<" y = "<<y<<endl;
      LL u0=(MP[x-1].find(y-1)!=MP[x-1].end());
      LL u1=(MP[x-1].find(y+1)!=MP[x-1].end());
      LL u2=(MP[x+1].find(y+1)!=MP[x+1].end());
      LL u3=(MP[x+1].find(y-1)!=MP[x+1].end());
      //      cout<<" u0 = "<<u0<<" u1 = "<<u1<<" u2 = "<<u2<<" u3 = "<<u3<<endl;
      if(u0&&u1)cnt++;
      if(u1&&u2)cnt++;
      if(u2&&u3)cnt++;
      if(u3&&u0)cnt++;
    }
    ans+=cnt;
    writeln(ans*3);
  }
  return 0;
}
/*
my test data:
input:
1
3 3
2 1
2 3
3 1
1 1 1
output:
3

input:
1
3 4
3 2
4 3
2 4
1 1 1 
output:
3

input:
1
3 3
3 2
1 2
2 1
1 1 1 
output:
3
*/
